﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class Ticket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Text = "";
            lblError2.Text = "";
            lblEmdError.Text = "";
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("SA2") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }

                if (Request.QueryString["tkt_no"] != null)
                {
                    txtTktNo.Text = Request.QueryString["tkt_no"];
                    lbtnQuery_Click(sender, e);
                }
                else if (Request.QueryString["bk_id"] != null && Request.QueryString["pax_sn"] != null)
                {
                    txtTktNo.Text = (Convert.ToInt64(Request.QueryString["bk_id"]) * 1000 + Convert.ToInt64(Request.QueryString["pax_sn"])).ToString();
                    lbtnQuery_Click(sender, e);
                }
                else
                {
                    if (Session["TKT_NO"] != null)
                    {
                        txtTktNo.Text = Session["TKT_NO"].ToString();
                        lbtnQuery_Click(sender, e);
                    }
                }
                btnCancel.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnRefund.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnChgPax.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnTransfer.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnIssueInvoice.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnRefund.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnCloseRefund.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnSetRefundFare.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnAddNote.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnCancelOneProduct.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnChgFlt.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnPayEmd.Attributes.Add("onclick", "javascript:return confirm('Please confirm the total pay! Are you sure?');");
                btnCancelEmd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
                btnRefundEmd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            string pax_sn, bk_id;
            txtTktNo.Text = txtTktNo.Text.Trim();
            Session["TKT_NO"] = txtTktNo.Text;
            if (txtTktNo.Text.Length != 15)
            {
                lblError.Text = "Invalid ticket number!";
                return;
            }
            pax_sn = txtTktNo.Text.Substring(txtTktNo.Text.Length - 3);
            bk_id = txtTktNo.Text.Substring(0, txtTktNo.Text.Length - 3);
            hidOrderNo.Value = bk_id;
            hidPaxSn.Value = pax_sn;
            hidOrderNo2.Value = bk_id;
            hidPaxSn2.Value = pax_sn;
            hidOrderNo3.Value = bk_id;
            hidPaxSn3.Value = pax_sn;
            GridView3.DataBind();
            GridView1.DataBind();
            grdMerchandise.DataBind();
            btnReceipts.Attributes.Add("onclick", "javascript:window.open('Itinerary.aspx?bk_id=" + bk_id + "&pax_sn=" + pax_sn + "');");
        }
        protected void lstAgeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstAgeType.SelectedValue == "A")
            {
                lblInfDob.Visible = false;
                txtInfantDob.Visible = false;
                chkHoldInfant.Visible = true;
                chkAloneChd.Checked = false;
                chkAloneChd.Visible = false;
            }
            else if (lstAgeType.SelectedValue == "I")
            {
                lblInfDob.Visible = true;
                txtInfantDob.Visible = true;
                chkHoldInfant.Checked = false;
                chkHoldInfant.Visible = false;
                chkAloneChd.Checked = false;
                chkAloneChd.Visible = false;
            }
            else
            {
                lblInfDob.Visible = false;
                txtInfantDob.Visible = false;
                chkHoldInfant.Checked = false;
                chkHoldInfant.Visible = false;
                chkAloneChd.Visible = true;
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            //任意段市场航空公司销售、座控部门可取消整张订单！
            int ret_code;
            if (lstCancelType.SelectedValue == "*")
            {
                lblError2.Text = "Please select cancel responsibility!";
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_cancel");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                //para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);//所有关于该旅客的票都自动退掉！
                //if(chkPassive2.Checked)
                //    para = cmd.Parameters.AddWithValue("@pax_passive", 1);
                //else
                //    para = cmd.Parameters.AddWithValue("@pax_passive", 0);
                para = cmd.Parameters.AddWithValue("@cancel_type", lstCancelType.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError2.Text = "Done.";
                    lbtnQuery_Click(sender, e);
                    //btnRefreshEMD(sender, e); 
                    //GridView3.DataBind();
                    //GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError2.Text = "Not proper office or ticket has been refunded!";
                else if (ret_code == 2)
                    lblError2.Text = "Check authority!";
                else if (ret_code == 3)
                    lblError2.Text = "Ticket not found, or not a Normal ticket!";
                else if (ret_code == 4)
                    lblError2.Text = "Can not convert to local time!";
                else if (ret_code == 90)
                    lblError2.Text = "Updating ticket failed!";
                else if (ret_code == 91)
                    lblError2.Text = "Updating flight cabin failed!";
                else if (ret_code == 92)
                    lblError2.Text = "Updating flight class failed!";
                //else if (ret_code == 95)
                //    lblError.Text = "Insertint request to queue failed!";
                else if (ret_code == 150)
                    lblError2.Text = "Calculate flight failed! Please calculate the flight manually!";
                else
                    lblError2.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError2.Text = ex.Message;
                else
                    lblError2.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnRefund_Click(object sender, EventArgs e)
        {
            //必须是已经整体取消并设置退款额的（或已使用）的票才能退款，只能一次，整个票状态将置为R。
            int ret_code;
            if (Session["ACL"].ToString().IndexOf("SA9") < 0)
            {
                lblError2.Text = "Unauthorized!";
                return;
            }
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //{
            //    lblError2.Text = "Please select the ticket.";
            //    return;
            //}
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_refund");//bll_tkt_refund_trip
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                //para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError2.Text = "Done.";
                    lbtnQuery_Click(sender, e);
                    //btnRefreshEMD(sender, e); 
                    //GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError2.Text = "update trip/emd failed!";
                else if (ret_code == 2)
                    lblError2.Text = "update tkt_root failed!";
                else
                    lblError2.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError2.Text = ex.Message;
                else
                    lblError2.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }
        protected void btnTransfer_Click(object sender, EventArgs e)
        {
            ////签转操作：旅客座位状态置为已签转出（T），迁转航班不在此系统结算！签转不对目标航班做任何操作，仅人工记录目标航班用于出报表！目标航班类似候补K位

            if (Session["ACL"].ToString().IndexOf("DB3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;

            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the pax trip you want to Transfer.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            try
            {
                //SqlCommand cmd = new SqlCommand("usp_transfer_out");
                SqlCommand cmd = new SqlCommand("usp_transfer_flt_pax");//签出，目标航班暂为系统外航班，另行结算！
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
                para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
                para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);//所有关于该旅客的票都自动退掉！
                if (chkPaxPassive.Checked)
                    para = cmd.Parameters.AddWithValue("@pax_passive", 1);
                else
                    para = cmd.Parameters.AddWithValue("@pax_passive", 0);
                para = cmd.Parameters.AddWithValue("@target_airline", txtX2Airline.Text);
                para = cmd.Parameters.AddWithValue("@target_flt_no", txtX2FltNo.Text);
                para = cmd.Parameters.AddWithValue("@target_flt_date", txtX2FltDate.Text);
                //para = cmd.Parameters.AddWithValue("@target_cabin", lstCabins.SelectedValue);
                //假设目标航班也存在原始航班航段！
                //para = cmd.Parameters.AddWithValue("@target_ori", lstOri.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@target_des", lstDes.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@cabin", lstCabins.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@class",lstClasses.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Transfer failed!";
                else if (ret_code == 2)
                    lblError.Text = "Ticket not found!";
                else if (ret_code == 150)
                    lblError.Text = "Transfer Done! Re-Calculate cabin failed.";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }

        protected void btnLogs_Click(object sender, EventArgs e)
        {
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //{
            //    lblError.Text = "Please select the ticket";
            //    return;
            //}
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');
            Response.Redirect("TicketLog.aspx?pnr=" + hidOrderNo.Value + "&pax_sn=" + hidPaxSn.Value);// + "&trip_sn=" + strArray[2]);
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            int ret_code;
            if (Session["ACL"].ToString().IndexOf("SA9") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //{
            //    lblError.Text = "Please select the ticket.";
            //    return;
            //}
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_reject_refund_request");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                //para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                //para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                    lblError.Text = "Rejected.";
                else if (ret_code == 1)
                    lblError.Text = "failed!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }


        protected void btnRefreshNotes_Click(object sender, EventArgs e)
        {
            GridView2.DataBind();
        }
        protected void btnAddNote_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0 && Session["ACL"].ToString().IndexOf("SA5") < 0 && Session["ACL"].ToString().IndexOf("SA6") < 0 
                && Session["ACL"].ToString().IndexOf("SA7") < 0 && Session["ACL"].ToString().IndexOf("SA8") < 0 && Session["ACL"].ToString().IndexOf("SA9") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_add_note");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo2.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn2.Value);
                para = cmd.Parameters.AddWithValue("@new_note", txtNewNote.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    txtNewNote.Text = "(Done)";
                    GridView2.DataBind();
                }
                else if (ret_code == 1)
                    txtNewNote.Text = "(Insert fail.)";
                else
                    txtNewNote.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    txtNewNote.Text = ex.Message;
                else
                    txtNewNote.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }
        protected void btnSetRefund_Click(object sender, EventArgs e)
        {
            //int ret_code;
            //if (Session["ACL"].ToString().IndexOf("BE0") < 0)
            //{
            //    lblError.Text = "Unauthorized!";
            //}
            //try
            //{
            //    SqlCommand cmd = new SqlCommand("usp_set_tkt_total_refund");
            //    SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
            //    para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
            //    para = cmd.Parameters.AddWithValue("@total_refund", txtTotalRefund.Text);
            //    para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            //    para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

            //    ret_code = TinyDAL.Exec(ref cmd);
            //    if (ret_code == 0)
            //    {
            //        lblError.Text = "Done.";
            //        GridView3.DataBind();
            //    }
            //    else if (ret_code == 1)
            //        lblError.Text = "Setting failed!";
            //    else
            //        lblError.Text = "Error:" + ret_code.ToString();
            //}
            //catch (Exception ex)
            //{
            //    lblError.Text = ex.Message;
            //}
        }
        protected void btnIssueInvoice_Click(object sender, EventArgs e)
        {
            int ret_code;
            if (Session["ACL"].ToString().IndexOf("SA9") < 0)
            {
                lblError2.Text = "Unauthorized!";
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_set_invoice_no");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                para = cmd.Parameters.AddWithValue("@invoice_no", txtInvoiceNo.Text);
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError2.Text = "Done.";
                    GridView3.DataBind();
                }
                else if (ret_code == 1)
                    lblError2.Text = "Failed! Check ticket status please!";
                else if (ret_code == 2)
                    lblError2.Text = "Invalid office!";
                else if (ret_code == 3)
                    lblError2.Text = "Setting failed!";
                else if (ret_code == 4)
                    lblError2.Text = "Invoice has been issued before! If you want to re-issue, please issue empty invoice number first!";
                else
                    lblError2.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError2.Text = ex.Message;
                else
                    lblError2.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnCloseRefund_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA9") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string pax_sn, bk_id;
            txtTktNo.Text = txtTktNo.Text.Trim();
            if (txtTktNo.Text.Length != 14)
            {
                lblError.Text = "Invalid ticket number!";
                return;
            }
            pax_sn = txtTktNo.Text.Substring(txtTktNo.Text.Length - 3);
            bk_id = txtTktNo.Text.Substring(0, txtTktNo.Text.Length - 3);
            int ret_code;
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_close_refund_request");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", bk_id);
                para = cmd.Parameters.AddWithValue("@pax_sn", pax_sn);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@user_office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    //GridView1.DataBind();
                    //GridView3.DataBind();
                    Response.Redirect("refund_queue2.aspx");
                }
                else if (ret_code == 1)
                    lblError.Text = "Updating queue status faile.";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnSetRefundFare_Click(object sender, EventArgs e)
        {
            int ret_code;
            if (Session["ACL"].ToString().IndexOf("SA9") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the ticket.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_set_trip_refund");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
                para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
                para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);
                para = cmd.Parameters.AddWithValue("@refund_tkt_fare", txtRefundTktFare.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Ticket not found!";
                else if (ret_code == 2)
                    lblError.Text = "Invalid order,improper order status, or wrong office!";
                else if (ret_code == 3)
                    lblError.Text = "Setting failed!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }
        protected void btnCancelOneProduct_Click(object sender, EventArgs e)
        {
            //目前发票是多段的，所以退款要整单回收，暂不支持部分退
            //如果不考虑发票问题，变更航班、签转是很普遍的业务需求。全退+重订是否可满足需求呢？
            //发票问题可以系统外解决，系统应提供这一功能！

            //任意段市场航空公司销售、座控部门可取消整张订单！
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }

            int ret_code;
            if (lstCancelType.SelectedValue == "*")
            {
                lblError.Text = "Please select cancel responsibility!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the ticket.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_cancel_fare_trips");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);//所有关于该产品的段都自动退掉！
                //if(chkPassive2.Checked)
                //    para = cmd.Parameters.AddWithValue("@pax_passive", 1);
                //else
                //    para = cmd.Parameters.AddWithValue("@pax_passive", 0);
                para = cmd.Parameters.AddWithValue("@cancel_type", lstCancelType.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView3.DataBind();
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Ticket not found!";
                else if (ret_code == 2)
                    lblError.Text = "Unauthorized!";
                else if (ret_code == 3)
                    lblError.Text = "Trip not found!";
                else if (ret_code == 90)
                    lblError.Text = "Updating ticket failed!";
                else if (ret_code == 91)
                    lblError.Text = "Updating flight cabin failed!";
                else if (ret_code == 92)
                    lblError.Text = "Updating flight class failed!";
                //else if (ret_code == 92)
                //    lblError.Text = "Cancel failed! Maybe ticket has just been cancelled.You can try again.";
                //else if (ret_code == 95)
                //    lblError.Text = "Insertint request to queue failed!";
                else if (ret_code == 150)
                    lblError.Text = "Calculate flight failed! Please calculate the flight manually!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnChgFlt_Click(object sender, EventArgs e)
        {
            //定义转入子舱，可不定义票价，专门接收变更转入（与离港签转一样）！？
            //有位才能转？！同舱转？ K/B？
            if (Session["ACL"].ToString().IndexOf("DB3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            if (chkForceTransfer.Checked && Session["ACL"].ToString().IndexOf("BD2") < 0)
            {
                lblError.Text = "Force transfer is unauthorized!";
                return;
            }
            txtChgFlt.Text = txtChgFlt.Text.Trim();
            if (txtChgFlt.Text.Length == 0)
                return;

            int ret_code;
            StringBuilder sSeg = new StringBuilder();
            StringBuilder sFlt = new StringBuilder();
            StringBuilder sDate = new StringBuilder();
            StringBuilder sOri = new StringBuilder();
            StringBuilder sDes = new StringBuilder();
            StringBuilder sClass = new StringBuilder();
            DateTime dt = new DateTime();
            string[] strArray, array2;
            txtChgFlt.Text = txtChgFlt.Text.ToUpper();
            strArray = txtChgFlt.Text.Split(',');
            int i, j, tmp;
            i = strArray.GetLength(0);
            for (j = 0; j < i; j++)
            {
                array2 = strArray[j].Split('/');
                if (array2.GetLength(0) != 6)
                {
                    lblError.Text = "Invalid format!";
                    return;
                }
                try
                {
                    dt = Convert.ToDateTime(array2[5]);
                    tmp = Convert.ToInt16(array2[0]);
                }
                catch (Exception ex)
                {
                    lblError.Text = "Invalid format!";
                    return;
                }
                sSeg.Append(array2[0] + ",");
                sFlt.Append(array2[1] + ",");
                sOri.Append(array2[2] + ",");
                sDes.Append(array2[3] + ",");
                sDate.Append(array2[5] + ",");
                sClass.Append(array2[4] + ",");
            }

            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_change_flt");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                para = cmd.Parameters.AddWithValue("@force_transfer", chkForceTransfer.Checked);
                para = cmd.Parameters.AddWithValue("@count", i);
                para = cmd.Parameters.AddWithValue("@seg_buf", sSeg.ToString());
                para = cmd.Parameters.AddWithValue("@flt_buf", sFlt.ToString());
                para = cmd.Parameters.AddWithValue("@date_buf", sDate.ToString());
                para = cmd.Parameters.AddWithValue("@ori_buf", sOri.ToString());
                para = cmd.Parameters.AddWithValue("@des_buf", sDes.ToString());
                para = cmd.Parameters.AddWithValue("@class_buf", sClass.ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Too many trips!";
                else if (ret_code == 2)
                    lblError.Text = "Bad format!";
                else if (ret_code == 3)
                    lblError.Text = "Target flight not found or not available!";
                else if (ret_code == 4)
                    lblError.Text = "Target flight segment not found!";
                else if (ret_code == 5)
                    lblError.Text = "Trip trip not found!";
                else if (ret_code == 6)
                    lblError.Text = "Cabin not found!";
                else if (ret_code == 7)
                    lblError.Text = "Class not found or not open!";
                else if (ret_code == 8)
                    lblError.Text = "No available seat!";
                else if (ret_code == 9)
                    lblError.Text = "Can not change to differrent city without super authority!";
                else if (ret_code == 10)
                    lblError.Text = "Can not change to differrent class without super authority!";
                else if (ret_code == 20)
                    lblError.Text = "Book target cabin failed!";
                else if (ret_code == 21)
                    lblError.Text = "Book target class failed!";
                else if (ret_code == 22)
                    lblError.Text = "Cancel origin cabin failed!";
                else if (ret_code == 23)
                    lblError.Text = "Cancel class class failed!";
                else if (ret_code == 30)
                    lblError.Text = "Change ticket flight failed!";
                else if (ret_code == 102)
                    lblError.Text = "Transfer done. But re-calculating nested failed, please re-calculate related flights manually!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnOrder_Click(object sender, EventArgs e)
        {
            Response.Redirect("Order.aspx?pnr=" + hidOrderNo.Value);
        }
        protected void chkChangePax_CheckedChanged(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA5") < 0)
            {
                lblError2.Text = "Unauthorized!";
                chkChangePax.Checked = false;
                return;
            }
            if (chkChangePax.Checked)
            {
                panChangePax.Visible = true;
                //retreive pax info...
                try
                {
                    int ret_code;
                    DataTable dtPaxName = new DataTable();
                    SqlCommand cmd = new SqlCommand("bll_tkt_get_root_info");
                    SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                    para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                    para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                    para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                    para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                    ret_code = TinyDAL.Exec(ref cmd, ref dtPaxName);
                    if (ret_code == 0)
                    {
                        lblError2.Text = "Done.";
                        if (dtPaxName.Rows.Count == 1)
                        {
                            DataRow row = dtPaxName.Rows[0];
                            txtFamilyNameEn.Text = row["family_name_en"].ToString().TrimEnd();
                            txtFamilyName.Text = row["family_name"].ToString().TrimEnd();
                            txtNameEn.Text = row["name_en"].ToString().TrimEnd();
                            txtName.Text = row["name"].ToString().TrimEnd();
                            chkHoldInfant.Checked = Convert.ToBoolean(row["hold_inf"]);
                            if (Convert.ToBoolean(row["male"]))
                                radMale.Checked = true;
                            else
                                radFemale.Checked = true;
                            lstAgeType.SelectedValue = row["age_type"].ToString();
                            if (Convert.IsDBNull(row["inf_dob"]))
                                txtInfantDob.Text = "";
                            else
                                txtInfantDob.Text = Convert.ToDateTime(row["inf_dob"]).ToShortDateString();
                            lstNations.SelectedValue = row["nationality"].ToString();
                            lstIdTypes.SelectedValue = row["id_type"].ToString().Trim();
                            txtIdNo.Text = row["id_no"].ToString().TrimEnd();
                            txtExpiry.Text = Convert.ToDateTime(row["id_expired_date"]).ToShortDateString();
                            txtPhone.Text = row["mobile_phone"].ToString().TrimEnd();
                            txtEmail.Text = row["email"].ToString().TrimEnd();
                            if (row["ffp_airline"].ToString() != "   ")
                                lstFfpAirlines.SelectedValue = row["ffp_airline"].ToString();
                            txtFfpNo.Text = row["ffp_no"].ToString().TrimEnd();
                            chkVip.Checked = Convert.ToBoolean(row["vip"]);
                            chkAloneChd.Checked = Convert.ToBoolean(row["alone_chd"]);
                            lstMoveAssistant.SelectedValue = row["move_assistant"].ToString();
                            lstMeal.SelectedValue = row["meal"].ToString();
                            txtRemark.Text = row["remark"].ToString().TrimEnd();
                            chkNeedInvoice.Checked = Convert.ToBoolean(row["need_invoice"]);
                            txtMailAddress.Text = row["mail_address"].ToString().TrimEnd();
                            txtPostCode.Text = row["post_code"].ToString().TrimEnd();

                        }
                    }
                    else if (ret_code == 1)
                        lblError2.Text = "Ticket not found!";
                    else if (ret_code == 2)
                        lblError2.Text = "No authority!";
                    else
                        lblError2.Text = "Error:" + ret_code.ToString();
                }
                catch (Exception ex)
                {
                    if (Application["DEBUG"].ToString() == "1")
                        lblError2.Text = ex.Message;
                    else
                        lblError2.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                }
            }
            else
                panChangePax.Visible = false;
        }
        protected void btnChgPax_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA5") < 0)
            {
                lblError2.Text = "Unauthorized!";
                return;
            }
            bool is_male;
            if (radMale.Checked)
                is_male = true;
            else if (radFemale.Checked)
                is_male = false;
            else
            {
                lblError2.Text = "Please select gender!";
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_change_pax");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                para = cmd.Parameters.AddWithValue("@ffp_airline", lstFfpAirlines.SelectedValue);
                para = cmd.Parameters.AddWithValue("@ffp_id", txtFfpNo.Text);
                para = cmd.Parameters.AddWithValue("@pax_value", txtPaxValue.Text);
                para = cmd.Parameters.AddWithValue("@family_name_en", txtFamilyNameEn.Text);
                para = cmd.Parameters.AddWithValue("@name_en", txtNameEn.Text);
                para = cmd.Parameters.AddWithValue("@family_name", txtFamilyName.Text);
                para = cmd.Parameters.AddWithValue("@name", txtName.Text);
                para = cmd.Parameters.AddWithValue("@hold_inf", chkHoldInfant.Checked);
                para = cmd.Parameters.AddWithValue("@male", is_male);
                //para = cmd.Parameters.AddWithValue("@age_type", lstAgeType.SelectedValue);
                //if (chkWithInf.Checked)
                //{
                //    para = cmd.Parameters.AddWithValue("@with_inf", true);
                //    para = cmd.Parameters.AddWithValue("@inf_name", txtInfName.Text);
                //    para = cmd.Parameters.AddWithValue("@inf_dob", txtInfantDob.Text);
                //}
                //else
                //{
                //    para = cmd.Parameters.AddWithValue("@with_inf", false);
                //    para = cmd.Parameters.AddWithValue("@inf_name", "");
                //    para = cmd.Parameters.AddWithValue("@inf_dob", "1900-1-1");
                //}
                para = cmd.Parameters.AddWithValue("@inf_dob", txtInfantDob.Text);
                para = cmd.Parameters.AddWithValue("@nationality", lstNations.SelectedValue);
                para = cmd.Parameters.AddWithValue("@id_type", lstIdTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@id_no", txtIdNo.Text);
                para = cmd.Parameters.AddWithValue("@id_expired_date", txtExpiry.Text);
                para = cmd.Parameters.AddWithValue("@mobile_phone", txtPhone.Text);
                para = cmd.Parameters.AddWithValue("@email", txtEmail.Text);
                para = cmd.Parameters.AddWithValue("@vip", chkVip.Checked);
                para = cmd.Parameters.AddWithValue("@alone_chd", chkAloneChd.Checked);
                para = cmd.Parameters.AddWithValue("@move_assistant", lstMoveAssistant.SelectedValue);
                para = cmd.Parameters.AddWithValue("@meal", lstMeal.SelectedValue);
                para = cmd.Parameters.AddWithValue("@remark", txtRemark.Text);
                para = cmd.Parameters.AddWithValue("@need_invoice", chkNeedInvoice.Checked);
                para = cmd.Parameters.AddWithValue("@mail_address", txtMailAddress.Text);
                para = cmd.Parameters.AddWithValue("@post_code", txtPostCode.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@user_office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                int ret_code;
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError2.Text = "Done.";
                    GridView3.DataBind();
                }
                else if (ret_code == 1)
                    lblError2.Text = "Ticket order not found!";
                else if (ret_code == 2)
                    lblError2.Text = "Ticket used!";
                else if (ret_code == 3)
                    lblError2.Text = "Updating failed!";
                else if (ret_code == 4)
                    lblError2.Text = "No authority!";
                else
                    lblError2.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError2.Text = ex.Message;
                else
                    lblError2.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnRetreiveMerchan_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA3") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_get_emd_detail_by_trip");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo3.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn3.Value);
                para = cmd.Parameters.AddWithValue("@trip_sn", lstTripSn.SelectedValue);
                para = cmd.Parameters.AddWithValue("@emd_id", lstEmdID.SelectedValue);
                para = cmd.Parameters.AddWithValue("@lang", Session["LANG"].ToString());
                DataTable tabRet = new DataTable();
                int ret_code;
                ret_code = TinyDAL.Exec(ref cmd, ref tabRet);
                if (ret_code == 0)
                {
                    if (tabRet.Rows.Count == 1)
                    {
                        lblCurrency.Text = tabRet.Rows[0]["currency"].ToString();
                        lblPrice.Text = tabRet.Rows[0]["price"].ToString();
                        txtEmdNote.Text = tabRet.Rows[0]["emd_note"].ToString();
                        lblUnit.Text = tabRet.Rows[0]["unit"].ToString();
                    }
                    else
                    {
                        lblCurrency.Text = "";
                        lblPrice.Text = "";
                        txtEmdNote.Text = "";
                        lblUnit.Text = "";
                        lblEmdError.Text = "Record not found!";
                    }
                }
                if (ret_code == 1)
                    lblEmdError.Text = "Ticket is not availiable!";
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void lstPayCurrencies_SelectedIndexChanged(object sender, EventArgs e)
        {


        }
        protected void lstEmdID_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (Session["ACL"].ToString().IndexOf("BA2") < 0)
            //{
            //    lblEmdError.Text = "Unauthorized!";
            //    return;
            //}
            //try
            //{
            //    SqlCommand cmd = new SqlCommand("usp_get_emd_fare_by_id");
            //    SqlParameter para = cmd.Parameters.AddWithValue("@airline", lstAirlines.SelectedValue);
            //    para = cmd.Parameters.AddWithValue("@emd_id", lstEmdID.SelectedValue);
            //    para = cmd.Parameters.AddWithValue("@lang", Session["LANG"].ToString());
            //    DataTable tabRet = new DataTable();
            //    int ret_code;
            //    ret_code = TinyDAL.Exec(ref cmd, ref tabRet);
            //    if (ret_code == 0)
            //    {
            //        if (tabRet.Rows.Count == 1)
            //        {
            //            lblCurrency.Text = tabRet.Rows[0]["currency"].ToString();
            //            lblPrice.Text = tabRet.Rows[0]["price"].ToString();
            //            txtEmdNote.Text = tabRet.Rows[0]["note"].ToString();
            //            //lstPayCurrencies_SelectedIndexChanged(sender, e);
            //        }
            //        else
            //        {
            //            lblCurrency.Text = "";
            //            lblPrice.Text = "";
            //            lblEmdTotal.Text = "";
            //            txtEmdNote.Text = "";
            //            lblEmdError.Text = "Record not found!";
            //        }
            //    }
            //    else
            //        lblEmdError.Text = "Error:" + ret_code.ToString();
            //}
            //catch (Exception ex)
            //{
            //    if (Application["DEBUG"].ToString() == "1")
            //        lblEmdError.Text = ex.Message;
            //    else
            //        lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            //}
        }

        protected void btnPayEmd_Click(object sender, EventArgs e)
        {
            //离线支付，交易额不转入本公司帐号。用于连接支付网关前的测试。
            if (lblEmdTotal.Text == "?" || lblEmdTotal.Text == "")
            {
                lblEmdError.Text = "Please calculate first!";
                return;
            }
            if (Session["ACL"].ToString().IndexOf("SA9") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_pay_emd_offline");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo3.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn3.Value);
                para = cmd.Parameters.AddWithValue("@pay_currency", lstPayCurrencies.SelectedValue);
                para = cmd.Parameters.AddWithValue("@pay_total", lblEmdTotal.Text);
                para = cmd.Parameters.AddWithValue("@sell_user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@sell_office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@sell_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                para = cmd.Parameters.AddWithValue("@ret_msg", SqlDbType.Char);
                para.Direction = ParameterDirection.Output;
                int ret_code;
                lblEmdTotal.Text = "";
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    grdMerchandise.DataBind();
                    lblEmdError.Text = "Done.";
                }
                else if (ret_code == 2)
                    lblEmdError.Text = "Update status failed!";
                else if (ret_code == 1)
                    lblEmdError.Text = "Exchange rate not found!";
                else if (ret_code == 3)
                    lblEmdError.Text = "Total pay mismatched!";
                else if (ret_code == 20)
                    lblEmdError.Text = "Service trip not found! -" + Convert.ToSingle(para.Value).ToString() + ", please remove the service!";
                else if (ret_code == 21)
                    lblEmdError.Text = "Service is unavailiable by now! -" + Convert.ToSingle(para.Value).ToString() + ", please remove the service!";
                else if (ret_code == 22)
                    lblEmdError.Text = "Service citycode not found! -" + Convert.ToSingle(para.Value).ToString() + ", please remove the service!";
                else if (ret_code == 23)
                    lblEmdError.Text = "Service is suspended! -" + Convert.ToSingle(para.Value).ToString() + ", please remove the service!";
                else if (ret_code == 100)
                    lblEmdError.Text = "Target currency not found!";
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnCancelEmd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }

            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["emdKEY"] == null)
            {
                lblEmdError.Text = "Please select the merchandise.";
                return;
            }
            key = Request.Form["emdKEY"];
            strArray = key.Split('|');
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_cancel_emd");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
                para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
                para = cmd.Parameters.AddWithValue("@cart_time", strArray[2]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblEmdError.Text = "Done.";
                    grdMerchandise.DataBind();
                }
                else if (ret_code == 1)
                    lblEmdError.Text = "Not proper office or ticket refunded!";
                else if (ret_code == 2)
                    lblEmdError.Text = "Authorize!";
                else if (ret_code == 3)
                    lblEmdError.Text = "Update failed!";
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnRefundEmd_Click(object sender, EventArgs e)
        {
            //如果EMD先部分退款，再整个机票取消/退款，只能通过退款时间区分两次EMD退款
            int ret_code;
            if (Session["ACL"].ToString().IndexOf("SA9") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["emdKEY"] == null)
            {
                lblEmdError.Text = "Please select the ticket.";
                return;
            }
            key = Request.Form["emdKEY"];
            strArray = key.Split('|');
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_refund_emd");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                para = cmd.Parameters.AddWithValue("@cart_time", strArray[2]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblEmdError.Text = "Done.";
                    grdMerchandise.DataBind();
                }
                else if (ret_code == 1)
                    lblEmdError.Text = "Failed!";
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void BtnAddToCart_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA3") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_add_emd");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo3.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn3.Value);
                para = cmd.Parameters.AddWithValue("@trip_sn", lstTripSn.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@airline", lstAirlines.SelectedValue);
                para = cmd.Parameters.AddWithValue("@emd_id", lstEmdID.SelectedValue);
                para = cmd.Parameters.AddWithValue("@quantity", txtQuantity.Text);
                para = cmd.Parameters.AddWithValue("@remark", txtEmdRemark.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                //para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                int ret_code;
                lblEmdTotal.Text = "";
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    grdMerchandise.DataBind();
                    lblEmdError.Text = "Done.";
                }
                else if (ret_code == 1)
                    lblEmdError.Text = "Ticket is not found or cancelled!";
                else if (ret_code == 3)
                    lblEmdError.Text = "Invalid Service!";
                else if (ret_code == 4)
                    lblEmdError.Text = "Adding failed!";
                else if (ret_code == 5)
                    lblEmdError.Text = "Flight not found!";
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnRemoveFromCart_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA3") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["emdKEY"] == null)
            {
                lblError.Text = "Please select the ticket.";
                return;
            }
            key = Request.Form["emdKEY"];
            strArray = key.Split('|');
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_remove_emd");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo3.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn3.Value);
                para = cmd.Parameters.AddWithValue("@cart_time", strArray[2]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                //para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                int ret_code;
                lblEmdTotal.Text = "";
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    grdMerchandise.DataBind();
                    lblEmdError.Text = "Done.";
                }
                else if (ret_code == 1)
                    lblEmdError.Text = "Failed to remove!";
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void lstAirlines_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstEmdID.DataBind();
        }

        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA3") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_calculate_emd_total_price");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo3.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn3.Value);
                para = cmd.Parameters.AddWithValue("@target_currency", lstPayCurrencies.SelectedValue);
                para = cmd.Parameters.Add("@total_pay", SqlDbType.Money);
                para.Direction = ParameterDirection.Output;
                int ret_code;
                lblEmdTotal.Text = "?";
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                    lblEmdTotal.Text = Convert.ToSingle(para.Value).ToString();
                else if (ret_code == 1)
                    lblEmdError.Text = "1:Exchange rate not found!";
                else if (ret_code == 100)
                    lblEmdError.Text = "100:Target currency not found!";
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnRefreshEMD(object sender, EventArgs e)
        {
            grdMerchandise.DataBind();
        }
        protected void lstTripSn_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstEmdID.DataBind();
        }
        protected void btnUse_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }

            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["emdKEY"] == null)
            {
                lblEmdError.Text = "Please select the merchandise.";
                return;
            }
            key = Request.Form["emdKEY"];
            strArray = key.Split('|');
            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_use_emd");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
                para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
                para = cmd.Parameters.AddWithValue("@cart_time", strArray[2]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblEmdError.Text = "Done.";
                    grdMerchandise.DataBind();
                }
                else if (ret_code == 1)
                    lblEmdError.Text = "Failed!";
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void lstLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnReceipts.Attributes.Clear();
            btnReceipts.Attributes.Add("onclick", "javascript:window.open('Itinerary.aspx?bk_id=" + hidOrderNo.Value + "&pax_sn=" + hidPaxSn.Value + "&lang=" + lstLang.SelectedValue + "');");
        }
        protected void lstLang_DataBound(object sender, EventArgs e)
        {
            lstLang.SelectedValue = Session["LANG"].ToString();
        }

        protected void btnBookFlt4OpenTrip_Click(object sender, EventArgs e)
        {
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the trip first!";
                return;
            }
            string key;
            string[] strArray;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("BookOpenTrip.aspx?bk_id=" + strArray[0] + "&pax_sn=" + strArray[1] + "&trip_sn=" + strArray[2]+"&flt_date="+txtFltDate.Text);
        }
        protected void btnSelectSeat_Click(object sender, EventArgs e)
        {
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the trip first!";
                return;
            }
            string key;
            string[] strArray;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("SelectSeat.aspx?bk_id=" + strArray[0] + "&pax_sn=" + strArray[1] + "&trip_sn=" + strArray[2]);
        }
    }
}